package com.example.suuu.project.system.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.suuu.model.PagVo;
import com.example.suuu.project.system.entity.SuMenu;
import com.example.suuu.project.system.entity.vo.UserCodeMenu;
import com.example.suuu.project.system.mapper.SuMenuMapper;
import com.example.suuu.project.system.query.SuMenuQuery;
import com.example.suuu.project.system.service.SuMenuService;
import com.example.suuu.tools.tree.TreeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* <p>
    * 菜单 服务实现类
    * </p>
*
* @author sun
* @date 2023-06-28
*/
@Transactional
@Service
public class SuMenuServiceImpl extends ServiceImpl<SuMenuMapper, SuMenu> implements SuMenuService{

    @Resource
    private SuMenuMapper suMenuMapper;



    @Override
    public Object getByUserCodeType(UserCodeMenu u) {
        List<SuMenu> list = new ArrayList<>();
        if(u.getUsercode().equals("999")){
            list = (List<SuMenu>) pagVo(new SuMenuQuery());
        }else{
            list = getByUserCode(u.getUsercode());
        }
        if (u.getType() != null) {
            if (u.getType().equals("tree")) {
                return TreeUtils.generateTrees(list);
            }
            List<String> arr = new ArrayList<>();
            if (u.getType().equals("codes")) {
                list.forEach(x -> arr.add(x.getMenucode()));
                return arr;
            }
            if (u.getType().equals("perms")) {
                list.forEach(x -> {
                    if (x.getPerms() != null && !x.getPerms().isEmpty()) {
                        arr.add(x.getPerms());
                    }
                });
                return arr;
            }
        }
        return list;
    }
    @Override
    public List<SuMenu> getByUserCode(String code) {
        return suMenuMapper.getByUserCode(code);
    }

    /**
    * 查询全部
    */
    @Override
    public List<SuMenu> getList(SuMenu u){
        return suMenuMapper.getList(u);
    };
    /**
    * 查询总数
    */
    @Override
    public Long count(SuMenu u){
        return suMenuMapper.count(u);
    };
    /**
    * 查询单个
    */
    @Override
    public SuMenu getById(Integer id){
        return suMenuMapper.getById(id);
    };
    /**
    * 新增
    */
    @Override
    public int add(SuMenu u){
        u.setIsdel(1);
        u.setMenucode(ObjectId.next());
        u.setCreatetime(LocalDateTime.now());
        u.setUpdatetime(LocalDateTime.now());
        return suMenuMapper.add(u);
    };
    /**
    * 修改
    */
    @Override
    public int update(SuMenu u){
        u.setUpdatetime(LocalDateTime.now());
        return suMenuMapper.update(u);
    };
    /**
    * 删除
    */
    @Override
    public int del(Integer id){
        return suMenuMapper.del(id);
    };

    /**
    * 分页区分方法
    */
    @Override
    public Object pagVo(SuMenuQuery query){
        SuMenu u = new SuMenu();
        u.setIsdel(1);
        if (query.getCurrentPage() != null && query.getPageSize() != null) {
            u.setCurrentPage((query.getCurrentPage()-1)*query.getPageSize());
            u.setPageSize(query.getPageSize());
            PagVo p = new PagVo();
            p.setCount(count(u));
            p.setList(getList(u));
            return p;
        }

        if(query.getType() !=null && query.getType().equals("tree")){
            return TreeUtils.generateTrees(getList(u));
        }
        return getList(u);
    }
}